/**
 * 专题图层 - 桥梁
 */
export default {
    data() {
        return {
        }
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        }
    },
    watch: {
        basin: {
            handler: function (name) {
                this.$nextTick(() => {
                    this.creatRHDC()
                })
            },
            immediate: true
        }
    },
    created() {
        this.entitiesRHDC = []
        this.entitiesRHDCMap = new Map()
        this.$eventBus.$on('flyToRHDC', this.flyToRHDC)
    },
    methods: {
        creatRHDC() {
            if (this.layerList2[1].children.RHDC.show) {
                this.clearRHDC()
                var url = `${mapConfig.resUrlPre}/geojson/${this.basin}/RHDC.json`
                this.$axios.get(url).then(res => {
                    res.data.features.forEach((item) => {
                        var coordinates = item.geometry.coordinates
                        var h = item.properties['宅基高'] + 25
                        // console.log(h)
                        var entity = {
                            eventName: 'RHDC',
                            lid: 'RHDC',
                            prop: item.properties,
                            position: Cesium.Cartesian3.fromDegrees(
                                // coordinates[0],
                                // coordinates[1],
                                item.properties['宅基经'],
                                item.properties['宅基纬'],
                                h
                            ),
                            billboard: {
                                image: this.layerList2[1].children.RHDC.icon,
                                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                                // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                                disableDepthTestDistance: 50000000,
                                width: this.$config.mapConfig.billboardSize,
                                height: this.$config.mapConfig.billboardSize
                            }
                        }
                        var key = item.properties['户主名'] + item.properties['宅基经'] + item.properties['宅基纬']
                        // this.entitiesRHDC.push(this.$viewer.entities.add(entity))
                        this.entitiesRHDCMap.set(key, this.$viewer.entities.add(entity))
                    })
                    // this.initEvent()
                })
            } else {
                this.clearRHDC()
            }
        },
        flyToRHDC(row) {
            var key = row.hOfHouse + row.lgtd + row.lttd
            console.log(row)
            this.$viewer.camera.setView({
                destination: Cesium.Cartesian3.fromDegrees(row.lgtd, row.lttd, 2000),
                orientation: {
                    heading: Cesium.Math.toRadians(0.0), // east, default value is 0.0 (north)
                    pitch: Cesium.Math.toRadians(-90), // default value (looking down)
                    roll: 0.0 // default value
                }
            })
            var entity = this.entitiesRHDCMap.get(key)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 100
                    }
                }).then(res => {
                    const popParams = this.$store.getters.popParams
                    var cart3 = Cesium.Cartesian3.fromDegrees(entity.prop['宅基经'], entity.prop['宅基纬'], entity.prop['宅基高'] + 25)
                    var xy = Cesium.SceneTransforms.wgs84ToWindowCoordinates(this.$viewer.scene, cart3)

                    popParams.name = '通用'
                    popParams.posX = Math.round(xy.x)
                    popParams.posY = Math.round(xy.y)
                    popParams.remarks = [
                        { name: '户主', value: entity.prop['户主名'] },
                        { name: '户数', value: entity.prop['户数(户)' + '户'] },
                        { name: '家庭人数', value: entity.prop['家庭人'] + '人' },
                        { name: '建筑面积', value: entity.prop['建筑面'] + 'm²' },
                        { name: '建筑类型', value: entity.prop['建筑类'] },
                        { name: '结构形态', value: entity.prop['结构形'] }
                    ]
                    this.$store.dispatch('popParams', popParams)
                })
            }
        },
        clearRHDC() {
            // if (this.entitiesRHDC && this.entitiesRHDC.length > 0) {
            //     this.entitiesRHDC.forEach(item => {
            //         this.$viewer.entities.remove(item)
            //     })
            //     this.entitiesRHDC = []
            // }
            if (this.entitiesRHDCMap && this.entitiesRHDCMap.size > 0) {
                this.entitiesRHDCMap.forEach((value, key) => {
                    this.$viewer.entities.remove(value)
                })
                this.entitiesRHDCMap = new Map()
            }
        },
        /**
         * 获取tip内容配置项
         * @param {Object} data 专题图层要素数据
         * @returns {Object}
         */
        getRemarksRHDC(data) {
            return [
                { name: null, value: data['O_Name'] }
                // { name: '房屋数', value: data.HOUSE },
                // { name: '人口数', value: data.PCOUNT },
            ]
        }
    }
}
